package com.fresh.liangyi.controller.client;

import com.fresh.liangyi.model.domain.Goods;
import com.fresh.liangyi.model.domain.Order;
import com.fresh.liangyi.model.domain.ShoppingCart;
import com.fresh.liangyi.model.domain.User;
import com.fresh.liangyi.service.GoodsService;
import com.fresh.liangyi.service.OrderService;
import com.fresh.liangyi.service.UserService;
import com.fresh.liangyi.utils.UserUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    private UserService userService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private OrderService orderService;

    @GetMapping(value = {"","/index"})
    public String index(@RequestParam(value = "page",defaultValue = "1") Integer page,
                        @RequestParam(value = "count",defaultValue = "10") Integer count,
                        Model model){
        List<Order> orderList=orderService.showOrderByUserId(UserUtils.getUser().getId());
        model.addAttribute("orderList",orderList);
        return "back/order_list";
    }
    //订单列表
    @GetMapping("/order")
    public String myOrder(@RequestParam(value = "page",defaultValue = "1") Integer page,
                          @RequestParam(value = "count",defaultValue = "10") Integer count,
                          Model model){
        List<Order> orderList=orderService.showOrderByUserId(UserUtils.getUser().getId());
        model.addAttribute("orderList",orderList);
        return "back/order_list";
    }

    //购物车
    @GetMapping("/cart")
    public String cart(Model model){
        User user=UserUtils.getUser();
        ShoppingCart shoppingCart=new ShoppingCart();
        //查询购物车中商品
        List<Goods> goodsList=goodsService.selectCartByUserId(user.getId());
        shoppingCart.setGoodsList(goodsList);
        double priceTotal=0;
        for (Goods goods:goodsList
             ) {
            priceTotal+=(goods.getDiscount()*goods.getPrice());
        }
        shoppingCart.setPriceTotal(priceTotal);
        model.addAttribute("shoppingCart",shoppingCart);
        return "back/shopping_cart";
    }
}
